<?php

namespace App\Http\Controllers\OpenShop\Feishu;

use App\Http\Controllers\BaseController;
use App\Models\System\SystemNoticesModel;
use App\Service\System\SystemNoticeService;
use Illuminate\Http\Request;

class SystemNoticeController extends BaseController
{
    public function index(Request $request)
    {
        $pageNum   = (int)$request->get('pageNum', 1);
        $pageSize  = (int)$request->get('pageSize', 20);
        //$userInfo = getRmsUserInfoByAuth();
        return $this->success(SystemNoticeService::list($pageNum, $pageSize));
    }

    public function save(Request $request)
    {
        $this->validate($request, [
            'title'            => 'required|string',
            'type'             => 'required|numeric',
            'startTime'        => 'required|string',
            'endTime'          => 'required|string',
            'presenceArea'     => 'required|numeric',
            'information'      => 'string',
            'additionalHide'   => 'string',
            'additionalShow'   => 'string',
            'coverImage'         => 'string',
            'detailImages'     => 'array',
            'id'               => 'numeric',
        ]);
        if ($request->type == SystemNoticesModel::NOTICE_TYPE_IMAGE) {
            $this->validate($request, [
                'coverImage'         => 'required|string',
                'detailImages'        => 'required|array',
            ]);
        }
        $userInfo        = getRmsUserInfoByAuth();
        $title           = $request->title;
        $type            = $request->type;
        $startTime       = $request->startTime ? strtotime($request->startTime) : 0;
        $endTime         = $request->endTime ? strtotime($request->endTime) : 0;
        $presenceArea    = $request->presenceArea;
        $content         = $request->information ?? '';
        $additionalHide  = $request->additionalHide ?? '';
        $additionalShow  = $request->additionalShow ?? '';
        $coverImg        = $request->coverImage ?? '';
        $detailImages    = $request->detailImages ?? [];
        $id              = $request->id ?? 0;
        SystemNoticeService::save($id, $title, $type, $startTime, $endTime, $presenceArea, $content, $additionalHide, $additionalShow, $coverImg, $detailImages, $userInfo);
        return $this->success();
    }

    public function del(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|numeric',
        ]);
        $id       = (int)$request->id;
        $userInfo = getRmsUserInfoByAuth();
        SystemNoticesModel::query()
            ->where('id', $id)
            ->update([
                'updatedByUid'   => $userInfo->id,
                'isDeleted'      => 1,
                'updatedAt'      => time(),
            ]);
        return $this->success();
    }

    public function detail(Request $request, $id)
    {
        return $this->success(SystemNoticeService::detail($id));
    }


    public function finish(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|numeric',
        ]);
        $pageNum   = (int)$request->get('pageNum', 1);
        $pageSize  = (int)$request->get('pageSize', 20);
        return $this->success(SystemNoticeService::finish($request->id, $pageNum, $pageSize));
    }
}
